package org.example.service;

import org.apache.commons.lang3.StringUtils;
import org.example.internalCommon.constant.CommonStatusEnum;
import org.example.internalCommon.constant.DriverCarConstants;
import org.example.internalCommon.constant.IdentityConstants;
import org.example.internalCommon.constant.TokenConstants;
import org.example.internalCommon.dto.ResponseResult;
import org.example.internalCommon.request.VerificationCodeDTO;
import org.example.internalCommon.response.DriverUserExistsResponse;
import org.example.internalCommon.response.NumberCodeResponse;
import org.example.internalCommon.response.TokenResponse;
import org.example.internalCommon.util.JwtUtils;
import org.example.internalCommon.util.RedisprefixUtils;
import org.example.remote.ServiceDriverUserClient;
import org.example.remote.ServiceVefificationcodeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class VerificationCodeService {

    @Autowired
    private ServiceDriverUserClient serviceDriverUserClient;

    @Autowired
    private ServiceVefificationcodeClient serviceVefificationcodeClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public ResponseResult checkAndSendVerificationCode(String driverPhone) {
        // 查询service-dricver-user 该手机号司机是否存在
        ResponseResult<DriverUserExistsResponse> driverUserExistsResponseResponseResult = serviceDriverUserClient.checkDriver(driverPhone);
        DriverUserExistsResponse data = driverUserExistsResponseResponseResult.getData();
        int ifExists = data.getIfExists();
        if (ifExists == DriverCarConstants.DRIVER_NOT_EXISTS) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXITST.getCode(),CommonStatusEnum.DRIVER_NOT_EXITST.getValue());
        }
        // 获取验证码
        ResponseResult<NumberCodeResponse> numberCode = serviceVefificationcodeClient.getNumberCode(6);
        // 调用第三方接口发送验证码
        // 存入redis
        // key
        String key = RedisprefixUtils.gemeratorKeyByPhone(driverPhone, IdentityConstants.DRIVER_IDENTITY);
        stringRedisTemplate.opsForValue().set(key, numberCode.getData().getNumberCode(), 2, TimeUnit.MINUTES);
        return ResponseResult.success();
    }

    /**
     *  校验验证码
     * @param verificationCodeDTO
     * @return
     */
    public ResponseResult checkCode(VerificationCodeDTO verificationCodeDTO) {
        String driverPhone = verificationCodeDTO.getDriverPhone();
        // 根据手机号，去redis拿到验证码
        String redisVerificationCode = stringRedisTemplate.opsForValue().get(RedisprefixUtils.gemeratorKeyByPhone(driverPhone, IdentityConstants.DRIVER_IDENTITY));
        // 校验验证码
        if (StringUtils.isBlank(redisVerificationCode) || !verificationCodeDTO.getVerificationCode().equals(redisVerificationCode)) {
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode()
                    , CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        // 颁发令牌
        String accessToken = JwtUtils.generatorToken(driverPhone, IdentityConstants.DRIVER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        String refreshToken = JwtUtils.generatorToken(driverPhone, IdentityConstants.DRIVER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);

        // token存到redis中
        String accessTokenKey = RedisprefixUtils.generatorTokenKey(driverPhone, IdentityConstants.DRIVER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        String refreshTokenKey = RedisprefixUtils.generatorTokenKey(driverPhone, IdentityConstants.DRIVER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);

        stringRedisTemplate.opsForValue().set(accessTokenKey, accessToken, 50, TimeUnit.MINUTES);
        stringRedisTemplate.opsForValue().set(refreshTokenKey, refreshToken, 200, TimeUnit.MINUTES);

        // 响应
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);

        return ResponseResult.success(tokenResponse);
    }
}
